using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZSW.Framework;
using Newtonsoft.Json;
using System.IO;
using NaughtyAttributes;
using Main.Event;
using System;

namespace Main.Save
{
    public class SaveLoadManager : ZSWF_Singleton<SaveLoadManager>
    {
        [HideInInspector] public DataSlot[] DataSlots;
        public List<ISaveable> SaveableList = new List<ISaveable>();
        private string jsonFolder;
        private int currentDataIndex;

        protected override void Awake()
        {
            base.Awake();
            jsonFolder = Application.streamingAssetsPath + "/save_data/";

            DataSlots = new DataSlot[3];

            ReadSaveData();

            MainEventSystem.StartNewGameEvent += OnStartNewGame;
        }
        public override void Init()
        {
            base.Init();
        }

        private void OnStartNewGame(int index)
        {
            currentDataIndex = index;
        }

        [Button]
        public void Test1()
        {
            Save(0);
        }

        [Button]
        public void Test2()
        {
            Load(0);
        }

        public void ReadSaveData()
        {
            if (!Directory.Exists(jsonFolder))
            {
                Directory.CreateDirectory(jsonFolder);
            }

            for (int i = 0; i < DataSlots.Length; i++)
            {
                var resultPath = jsonFolder + "data" + i + ".json";
                if (File.Exists(resultPath))
                {
                    var json = File.ReadAllText(resultPath);
                    var jsonData = JsonConvert.DeserializeObject<DataSlot>(json);
                    DataSlots[i] = jsonData;
                }
                else
                {
                    DataSlots[i] = null;
                }
            }
        }


        public void RegisterSaveable(ISaveable saveable)
        {
            if (!SaveableList.Contains(saveable))
            {
                SaveableList.Add(saveable);
            }
        }

        private void Save(int index)
        {
            DataSlot data = new DataSlot();

            foreach (var item in SaveableList)
            {
                data.DataDict.Add(item.GUID, item.GenerateSaveData());
            }

            DataSlots[index] = data;

            var path = jsonFolder + "data" + index + ".json";

            var jsonData = JsonConvert.SerializeObject(DataSlots[index]);

            if (!File.Exists(path))
            {
                Directory.CreateDirectory(jsonFolder);
            }

            File.WriteAllText(path, jsonData);
        }

        public void Load(int index)
        {
            currentDataIndex = index;

            var path = jsonFolder + "data" + currentDataIndex + ".json";

            var json = File.ReadAllText(path);

            var data = JsonConvert.DeserializeObject<DataSlot>(json);

            foreach (var item in SaveableList)
            {
                item.RestoreData(data.DataDict[item.GUID]);
            }
        }
    }
}